<?php

namespace app\admin\model;

use think\facade\Db;

/**
 * 行为日志-模型
 * @author tangsan
 * Class ActionLog
 * @package app\admin\model
 */
class ActionLog extends BaseModel
{
    // 设置数据表
    protected $table = null;
    // 自定义日志标题
    protected static $title = '';
    // 自定义日志内容
    protected static $content = '';

    /**
     * 构造函数
     * @param array $data
     * ActionLog constructor.
     * @author tangsan
     */
    public function __construct(array $data = [])
    {
        parent::__construct($data);
        // TODO:设置表名暂时不新建表（需要找到sqlsrv的新建表方式）
//        $this->table = DB_PREFIX . 'action_log_' . date('Y') . '_' . date('m');
        $this->table = DB_PREFIX . 'action_log_2021_10';
        // 初始化行为日志表
//        $this->initTable();
    }

    /**
     * 初始化日志表
     * @return string|null
     * @throws \think\db\exception\BindParamException
     * @author tangsan
     */
    private function initTable()
    {
        $tbl = $this->table;
        if (!$this->tableExists($tbl)) {
            switch (env('database.type', 'mysql')) {
                case 'sqlsrv':
                    $sql = "CREATE TABLE [dbo].[{$tbl}] (
                      [id] int IDENTITY(1,1) NOT NULL,
                      [username] varchar(60) NULL,
                      [method] varchar(20) NULL,
                      [module] varchar(30) NULL,
                      [url] varchar(200) NULL,
                      [param] text NULL,
                      [title] varchar(100) NULL,
                      [type] tinyint DEFAULT 0 NULL,
                      [content] varchar(1000) NULL,
                      [ip] varchar(18) NULL,
                      [ip_city] varchar(150) NULL,
                      [os] varchar(50) NULL,
                      [browser] varchar(255) NULL,
                      [user_agent] varchar(360) NULL,
                      [create_user] int DEFAULT 0 NULL,
                      [create_time] int NULL,
                      [mark] tinyint DEFAULT 1 NOT NULL,
                      PRIMARY KEY CLUSTERED ([id])
                    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
                    );";
                    break;
                default:
                    $sql = "CREATE TABLE `{$tbl}` (
                  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '唯一性标识',
                  `username` varchar(60) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '操作人用户名',
                  `method` varchar(20) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '请求类型',
                  `module` varchar(30) DEFAULT NULL COMMENT '模型',
                  `url` varchar(200) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '操作页面',
                  `param` text CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '请求参数(JSON格式)',
                  `title` varchar(100) DEFAULT NULL COMMENT '日志标题',
                  `type` tinyint(1) unsigned DEFAULT 0 COMMENT '操作类型：1登录系统 2注销系统 3操作日志',
                  `content` varchar(1000) DEFAULT '' COMMENT '内容',
                  `ip` varchar(18) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT 'IP地址',
                  `ip_city` varchar(150) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT 'IP所属城市',
                  `os` varchar(50) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '操作系统',
                  `browser` varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '浏览器',
                  `user_agent` varchar(360) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT 'User-Agent',
                  `create_user` int(11) unsigned DEFAULT 0 COMMENT '添加人',
                  `create_time` int(11) unsigned DEFAULT 0 COMMENT '添加时间',
                  `mark` tinyint(1) unsigned NOT NULL DEFAULT 1 COMMENT '有效标识：1正常 0删除',
                  PRIMARY KEY (`id`) USING BTREE
                ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 COMMENT='系统行为日志表';";
                    break;
            }
            Db::query($sql);
        }
        return $tbl;
    }

    /**
     * 设置标题
     * @param $title 标题
     * @author tangsan
     */
    public static function setTitle($title)
    {
        self::$title = $title;
    }

    /**
     * 设置内容
     * @param $content 内容
     * @author tangsan
     */
    public static function setContent($content)
    {
        self::$content = $content;
    }

    /**
     * 创建日志
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @author tangsan
     */
    public static function record()
    {
        if (!self::$title) {
            // 操作控制器名
            $menuModel = new Menu();
            $info = $menuModel->getOne([
                ['path', '=', request()->url()],
            ]);
            if ($info) {
                if ($info['type'] == 1) {
                    $menuInfo = $menuModel->getInfo($info['pid']);
                    self::$title = $menuInfo['title'];
                } else {
                    self::$title = $info['title'];
                }
            }
        }
        // 日志数据
        $data = [
            'username' => '管理员',
            'module' => app('http')->getName(),
            'method' => request()->method(),
            'url' => request()->url(true), // 获取完成URL
            'param' => request()->param() ? json_encode(request()->param()) : '',
            'title' => self::$title ? self::$title : '操作日志',
            'type' => self::$title == '登录系统' ? 1 : (self::$title == '注销系统' ? 2 : 3),
            'content' => self::$content,
            'ip' => request()->ip(),
//            'ip_city' => get_ip_city(request()->ip()),
            'os' => get_os(),
            'browser' => get_browse(),
            'user_agent' => request()->server('HTTP_USER_AGENT'),
            'create_user' => empty(session('userId')) ? 0 : session('userId'),
            'create_time' => time(),
        ];
        // 日志入库
        self::insert($data);
    }

}